package com.example.wisdommanufactor.fragment;

import android.graphics.Bitmap;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

import com.example.wisdommanufactor.R;

public class MyFragment extends BaseFragment {
    private static final String TAG = "MyFragment";
    WebView mWebView;
    @Override
    public Object setLayout() {
        return R.layout.fragment_my;
    }

    @Override
    public void onBindView(@Nullable Bundle saveInstanceState, View rootView) {
        mWebView = rootView.findViewById(R.id.wv_main);
        mWebView.getSettings().setJavaScriptEnabled(true);
        //mWebView.loadUrl("https://m.baidu.com");
        mWebView.loadUrl("file:///android_asset/hello.html");
        mWebView.setWebViewClient(new MyWebViewClient());
        mWebView.addJavascriptInterface(this,"android");
    }

    @JavascriptInterface
    public void startFragment(String target){
        switch (target){
            case "工厂灯光":
                Toast.makeText(getContext(), "工厂灯光", Toast.LENGTH_SHORT).show();
                start(new FactoryLightFragment());
                break;
            case "TEST":
                Toast.makeText(getContext(), "Test", Toast.LENGTH_SHORT).show();
                break;
        }
    }
    private class MyWebViewClient extends WebViewClient{
        @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            view.loadUrl(request.getUrl().toString());
            return true;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            Log.e(TAG, "onPageStarted: ");
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            super.onPageFinished(view, url);
            Log.e(TAG, "onPageFinished: " );
        }
    }
}
